<template>
  <UApp>
    <div class="app-container">
      <!-- <NuxtRouteAnnouncer />
    <NuxtWelcome /> -->
      <div class="flex-1 w-[100%]">
        <NuxtPage @update-footer-style="updateFooterStyle" />
      </div>
      <Footer :style="footerStyle" />
      <FloatingMenu />
    </div>
  </UApp>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import Footer from '~/components/footer.vue'
import FloatingMenu from '~/components/floating-menu.vue'
// 定义背景颜色和字体颜色
const backgroundColor = ref('#2F274C')
const textColor = ref('#FFFFFF')

// 计算动态样式
const footerStyle = computed(() => ({
  backgroundColor: backgroundColor.value,
  color: textColor.value,
}))

// 更新背景颜色和字体颜色的方法
const updateFooterStyle = (newBackgroundColor, newTextColor) => {
  backgroundColor.value = newBackgroundColor
  textColor.value = newTextColor
}

import { useState } from '#app'

import md5 from 'js-md5'
// 导入 userApi 实例
import userApi from '@/api/index'
import configApi from '@/api/config'
// const homeNews = useState('homeNews', () => [])
const newsCount = useState('newsCount', () => 0)
// const msgs = useState('msgs', () => [])
const configs = useState('configs', () => [])
async function handleConfig(robot_id) {
  try {
    const response = await configApi.getConfigList(`robot_id=${robot_id}`)
    if (response) {
      const res = JSON.parse(response)
      configs.value = res.data
    }
    // 处理登录成功后的逻辑，例如保存 token
  } catch (error) {
    console.error('getNews failed:', error)
    // 处理登录失败后的逻辑
  }
}

// 定义一个异步函数来处理登录
async function handleLogin() {
  try {
    const res = await userApi.login(
      `username=officialwebsite&password=${md5('grawav123')}`,
    )
    if (res) {
      // const res = JSON.parse(response)
      // 保存 Token 到 localStorage
      // 处理登录成功后的逻辑，例如保存 token
      if (res?.data?.id) {
        localStorage.setItem('user_id', res.data.id)
        localStorage.setItem('access_token', res.data.access_token)
        localStorage.setItem('current_robot_id', res.data.robot_id)
        handleConfig(res.data.robot_id)
        handleNewsCount(res?.data?.id)
      }
    }
  } catch (error) {
    console.error('Login failed:', error)
    handleLogin()
    // 处理登录失败后的逻辑
  }
}
// async function handleMsg(userId) {
//   try {
//     const response = await userApi.askQuestions(
//       `data=幻引AI小助手常驻意图&session_id=1&endpoint_id=${userId}&是否有效=1`,
//     )
//     if (response) {
//       const res = JSON.parse(response)
//       // console.log('getNews successful:', res.data)
//       const action_text = res.data.action_text
//         .replaceAll('</br>', '')
//         .split('\n')
//       // console.log(action_text)
//       msgs.value = action_text
//     }
//   } catch (error) {
//     console.error('getNews failed:', error)
//     // 处理登录失败后的逻辑
//   }
//   handleNewsCount(userId)
// }
// async function handleNews(userId) {
//   try {
//     const response = await userApi.askQuestions(
//       `data=查看新闻列表，根据发布日期倒排，每页9条，第1页&session_id=1&endpoint_id=${userId}&是否有效=1`,
//     )
//     if (response) {
//       const res = JSON.parse(response)
//       // console.log('getNews successful:', res.data)
//       const action_text = JSON.parse(res.data.action_text)
//       // console.log(action_text.data)
//       homeNews.value = action_text.data
//     }
//   } catch (error) {
//     console.error('getNews failed:', error)
//     // 处理登录失败后的逻辑
//   }
//   handleNewsCount(userId)
// }

async function handleNewsCount(userId) {
  try {
    const response = await userApi.askQuestions(
      `data=共有多少条新闻资讯&session_id=1&endpoint_id=${userId}&是否有效=1`,
    )
    if (response) {
      const res = JSON.parse(response)
      newsCount.value = res.data.action_text - 0
      // console.log(action_text.data)
    }
  } catch (error) {
    console.error('handleNewsCount failed:', error)
    // 处理登录失败后的逻辑
  }
}
onMounted(() => {
  nextTick(() => {
    handleLogin()
  })
})
</script>
